<?php

class ProjectController extends Zend_Controller_Action
{
    protected $_session;
    protected $_konsumen;
    protected $_pegawai;
    protected $_genset;
    protected $_proyek;
    protected $_konsumenList;
    protected $_pegawaiList;
    protected $_gensetList;
    
    public function init()
    {
        $this->_session = new Application_Model_Session();
        $this->_proyek = new Application_Model_Proyek();
        $this->_pegawaiList = new Application_Model_Pekerja();
        $this->_konsumenList = new Application_Model_Konsumen();
        $this->_gensetList = new Application_Model_Genset();
        $this->view->site = $this->getRequest()->getControllerName();
        $this->view->action = $this->getRequest()->getActionName();
    }

    public function indexAction()
    {
        $project = new Application_Model_Proyek();
        $this->view->proyekPending = $project->getAllDataCategories('PENDING');
        $this->view->proyekOnGoing = $project->getAllDataCategories('ON GOING');
        $this->view->proyekClosed = $project->getAllDataCategories('CLOSED');
        $this->view->proyekCanceled = $project->getAllDataCategories('CANCELED');
    }

    public function addAction()
    {
        $form = new Application_Form_Project();
        $form->getSubForm('summary')->removeDecorator('form');
        $form->getSubForm('summary')->removeDecorator('htmlTag');
        
        $form->getSubForm('waktu')->removeDecorator('form');
        $form->getSubForm('waktu')->removeDecorator('htmlTag');
        
        $form->getSubForm('anggota')->removeDecorator('form');
        $form->getSubForm('anggota')->removeDecorator('htmlTag');
        
        $form->getSubForm('konsumen')->removeDecorator('form');
        $form->getSubForm('konsumen')->removeDecorator('htmlTag');
        
        $form->getSubForm('genset')->removeDecorator('form');
        $form->getSubForm('genset')->removeDecorator('htmlTag');
        
        $formSummary = $form->getSubForm('summary');
        $formWaktu = $form->getSubForm('waktu');
        $formAnggota = $form->getSubForm('anggota');
        $formKonsumen = $form->getSubForm('konsumen');
        $formGenset = $form->getSubForm('genset');

        $this->view->summary = $formSummary;
        $this->view->waktu = $formWaktu;
        $this->view->anggota = $formAnggota;
        $this->view->konsumen = $formKonsumen;
        $this->view->genset = $formGenset;
        
        $this->view->catatan = $form->getElement('catatan_proyek');
        
        $this->view->submit = $form->getElement('submit');
        $this->view->cancel = $form->getElement('batal');
        
        $error = $this->_getParam('success');
        if($error == 'no'){ 
            $this->view->error = $error; 
        }
        
        if($this->_request->isPost())
        {
            if($form->isValid($this->_request->getPost()))
            {
                $id_status_project = $formSummary->getValue('id_status_proyek');
                
                $konsumen = $this->getKonsumen();
                $pegawai = $this->getPegawai();
                $genset = $this->getGenset();
                
                $id_konsumen = null;
                if($konsumen) {
                    foreach($konsumen as $konsumen)
                    {
                        $id_konsumen = $konsumen['id_konsumen'];
                    }
                }
                
                if($id_status_project == '1')
                {
                    if(!$konsumen && !$pegawai && !$genset)
                    {
                        $this->_redirect('project/add/success/no');
                        
                    }
                }
                
                $data = array(
                    'nama_proyek' => $formSummary->getValue('nama_proyek'),
                    'tanggal_mulai' => $formSummary->getValue('tanggal_mulai'),
                    'tanggal_selesai' => $formSummary->getValue('tanggal_selesai'),
                    'progress_proyek' => $formSummary->getValue('progress_proyek'),
                    'rencana_waktu_berlangsung' => $formWaktu->getValue('rencana_waktu_berlangsung'),
                    'waktu_terlaksana' => $formWaktu->getValue('waktu_terlaksana'),
                    'waktu_kurang' => $formWaktu->getValue('waktu_kurang'),
                    'catatan_proyek' => $form->getValue('catatan_proyek'),
                    'id_konsumen' => $id_konsumen,
                    'id_status_proyek' => $formSummary->getValue('id_status_proyek')
                );
                
                $proyekModel = new Application_Model_Proyek();
                $id_proyek = $proyekModel->proyekBaru($data);
                
                $pekerjaModel = new Application_Model_Pekerja();
                $pegawaiModel = new Application_Model_Pegawai();
                
                $id_manager = $formSummary->getValue('id_manager');
                
                $data = array(
                    'id_pegawai' => $id_manager,
                    'tanggung_jawab' => 'Manajer Proyek',
                    'id_proyek' => $id_proyek
                );

                $pekerjaModel->pekerjaBaru($data);
                
                $data = array(
                        'status_pekerjaan' => '1'
                );
                    
                $pegawaiModel->pegawaiUpdate($data, $id_manager);
                
                
                foreach($pegawai as $pegawai)
                {
                    $id_pegawai = $pegawai['id_pegawai'];
                    if($pekerjaModel->checkPekerja($id_pegawai))
                    {
                        $data = array(
                            'id_pegawai' => $id_pegawai,
                            'tanggung_jawab' => 'Staff',
                            'id_proyek' => $id_proyek
                        );

                        $pekerjaModel->pekerjaBaru($data);

                        $data = array(
                            'status_pekerjaan' => '1'
                        );

                        $pegawaiModel->pegawaiUpdate($data, $id_pegawai);
                        echo 'Berhasil !';
                    }
                }
                                
                $gensetModel = new Application_Model_Genset();
                
                
                if($genset) {
                    foreach($genset as $genset)
                    {
                        $data = array(
                        'status_genset' => 'Terpakai',
                        'id_proyek' => $id_proyek  
                        );
                        $gensetModel->gensetUpdate($data, $genset['id_genset']);
                    }
                }
                
                $this->clearSession();
                $this->_redirect('project/index');

            } else {
                $form->populate($this->_request->getPost());
                $this->view->pegawai = $this->getPegawai();
                $this->view->dataKonsumen = $this->getKonsumen();
                $this->view->dataGenset = $this->getGenset();
                $formSummary->getValue('tanggal_mulai');
                $formSummary->getValue('tanggal_selesai');
            }
        } else {
            $this->clearSession();
        }
        
    }

    public function deleteAction()
    {
        // action body
    }

    public function viewAction()
    {
        $id = $this->_getParam('id');
        if($id == null)
            $this->_redirect ('project');
        else {
            $form = new Application_Form_Project();
            $form->getSubForm('summary')->removeDecorator('form');
            $form->getSubForm('summary')->removeDecorator('htmlTag');
            $form->getSubForm('summary')->addAttribs(array('disabled' => true));
            $form->getSubForm('summary')->getElement('nama_proyek')->setAttrib('disabled', true);
            $form->getSubForm('summary')->getElement('tanggal_mulai')->setAttrib('disabled', true);
            $form->getSubForm('summary')->getElement('tanggal_selesai')->setAttrib('disabled', true);
            $form->getSubForm('summary')->getElement('id_manager')->setAttrib('disabled', true);
            $form->getSubForm('summary')->getElement('id_status_proyek')->setAttrib('disabled', true);

            $form->getSubForm('waktu')->removeDecorator('form');
            $form->getSubForm('waktu')->removeDecorator('htmlTag');
            $form->getSubForm('waktu')->getElement('rencana_waktu_berlangsung')->setAttrib('disabled', true);
            $form->getSubForm('waktu')->getElement('jam_kerja')->setAttrib('disabled', true);
            $form->getSubForm('waktu')->getElement('waktu_terlaksana')->setAttrib('disabled', true);
            $form->getSubForm('waktu')->getElement('waktu_kurang')->setAttrib('disabled', true);

            $formSummary = $form->getSubForm('summary');
            $formWaktu = $form->getSubForm('waktu');

            $this->view->summary = $formSummary;
            $this->view->waktu = $formWaktu;

            $this->view->catatan = $form->getElement('catatan_proyek')->setAttrib('disabled', true);

            $id = $this->_getParam('id');
            
            $this->view->id_project = $id;
            
            $proyek = $this->_proyek->getData($id);
            $form->populate($proyek);

            $manager = $this->_pegawaiList->getDataManager($id);

            foreach ($manager as $manager);
            
            if($manager != null) {
                $data = array( 'id_manager' => $manager['id_pegawai']);
                $formSummary->populate($data);
            }
            
            $id_konsumen = $proyek['id_konsumen'];

            if($id_konsumen) {
                $konsumen = $this->_konsumenList->getData($id_konsumen);
                $this->view->dataKonsumen = $konsumen;
            }
            

            $pegawai = $this->_pegawaiList->getData($id);

            $this->view->pegawai = $pegawai;
            

            $this->view->dataGenset = $this->_gensetList->getData($id);
            
        }
    }

    public function editAction()
    {   
        $id = $this->_getParam('id');
        if($id == null)
            $this->_redirect ('project');
        else {
            $form = new Application_Form_Project();
            $form->getSubForm('summary')->removeDecorator('form');
            $form->getSubForm('summary')->removeDecorator('htmlTag');

            $form->getSubForm('waktu')->removeDecorator('form');
            $form->getSubForm('waktu')->removeDecorator('htmlTag');

            $form->getSubForm('anggota')->removeDecorator('form');
            $form->getSubForm('anggota')->removeDecorator('htmlTag');

            $form->getSubForm('konsumen')->removeDecorator('form');
            $form->getSubForm('konsumen')->removeDecorator('htmlTag');

            $form->getSubForm('genset')->removeDecorator('form');
            $form->getSubForm('genset')->removeDecorator('htmlTag');

            $formSummary = $form->getSubForm('summary');
            $formWaktu = $form->getSubForm('waktu');
            $formAnggota = $form->getSubForm('anggota');
            $formKonsumen = $form->getSubForm('konsumen');
            $formGenset = $form->getSubForm('genset');

            $this->view->summary = $formSummary;
            $this->view->waktu = $formWaktu;
            $this->view->anggota = $formAnggota;
            $this->view->konsumen = $formKonsumen;
            $this->view->genset = $formGenset;
            
            $error = $this->_getParam('success');
            if($error == 'no'){ $this->view->error = $error; }
            
            $this->view->catatan = $form->getElement('catatan_proyek');

            $this->view->submit = $form->getElement('submit')->setLabel("Save Project");
            $this->view->cancel = $form->getElement('batal');
            $this->view->id_proyek = $form->getElement('id_proyek');
            
            if($this->_request->isPost())
            {
                if($form->isValid($this->_request->getPost()))
                {
                    $id_status_project = $formSummary->getValue('id_status_proyek');
                    
                    $proyek = $this->_proyek->getData($id);
                    
                    $id_konsumen = null;
                    
                    /*Begin Konsumen*/
                    
                    if($proyek['id_konsumen'])
                    {
                        $id_konsumen = $proyek['id_konsumen'];
                    }
                    
                    $konsumen = $this->getKonsumen();
                    
                    if($konsumen)
                    {
                        foreach($konsumen as $konsumen)
                        {
                            $id_konsumen = $konsumen['id_konsumen'];
                        };
                    }
                    
                    /*End Konsumen*/
                    
                    /*Begin Pegawai*/
                    $pekerjaModel = new Application_Model_Pekerja();
                    $pegawaiModel = new Application_Model_Pegawai();
                    $pegawai = $this->getPegawai();
                    /*End Pegawai*/
                    
                    /*Begin Genset*/
                    $gensetModel = new Application_Model_Genset();
                    $genset = $this->getGenset();
                    /*End Genset*/
                    if($id_status_project == '1')
                    {
                        if(!$konsumen && !$pegawai && !$genset)
                        {
                            if(!$pekerjaModel->getProject($id) || $konsumen ==null || $gensetModel->getData($id) == null)
                                $this->_redirect('project/edit/id/'.$id.'/success/no');
                        }
                    }
                    
                    /*Begin Proyek*/
                    
                    $dataProyek = array(
                        'nama_proyek' => $formSummary->getValue('nama_proyek'),
                        'tanggal_mulai' => $formSummary->getValue('tanggal_mulai'),
                        'tanggal_selesai' => $formSummary->getValue('tanggal_selesai'),
                        'progress_proyek' => $formSummary->getValue('progress_proyek'),
                        'rencana_waktu_berlangsung' => $formWaktu->getValue('rencana_waktu_berlangsung'),
                        'waktu_terlaksana' => $formWaktu->getValue('waktu_terlaksana'),
                        'waktu_kurang' => $formWaktu->getValue('waktu_kurang'),
                        'catatan_proyek' => $form->getValue('catatan_proyek'),
                        'id_konsumen' => $id_konsumen,
                        'id_status_proyek' => $formSummary->getValue('id_status_proyek')
                    );
                    
                    $id_proyek = $form->getValue('id_proyek');
                    
                    $proyekModel = new Application_Model_Proyek();
                    $proyekModel->proyekUpdate($dataProyek, $id_proyek);
                    
                    /*End Proyek*/
                    
                    /*Begin Manager*/
                    
                    $id_manager = $formSummary->getValue('id_manager');
                    
                    $dataManager = array(
                        'id_pegawai' => $id_manager,
                        'tanggung_jawab' => 'Manajer Proyek',
                        'id_proyek' => $id_proyek
                    );
                    
                    if(!$pekerjaModel->getDataManager($id_proyek))
                        $pekerjaModel->pekerjaBaru($dataManager);
                    else
                        $pekerjaModel->gantiManager ($dataManager, $id_proyek);
                
                    $dataStatusPegawai = array(
                            'status_pekerjaan' => '1'
                    );

                    $pegawaiModel->pegawaiUpdate($dataStatusPegawai, $id_manager);
                    /*End Manager*/
                    
                    /*Begin Update Pegawai*/
                    foreach($pegawai as $pegawai)
                    {
                        $id_pegawai = $pegawai['id_pegawai'];
                        
                        if($pekerjaModel->checkPekerja($id_pegawai))
                        {
                            $data = array(
                                'id_pegawai' => $id_pegawai,
                                'tanggung_jawab' => 'Staff',
                                'id_proyek' => $id_proyek
                            );

                            $pekerjaModel->pekerjaBaru($data);

                            $data = array(
                                'status_pekerjaan' => '1'
                            );

                            $pegawaiModel->pegawaiUpdate($data, $id_pegawai);
                        }
                    }
                    /*End Update Pegawai Pegawai*/
                    
                    /*Begin Update Data Genset*/
                    foreach($genset as $genset)
                    {
                        $data = array(
                        'status_genset' => 'Terpakai',
                        'id_proyek' => $id_proyek  
                        );
                        $gensetModel->gensetUpdate($data, $genset['id_genset']);
                    }
                    /*End Update Data Genset*/
                    
                    $this->clearSession();
                    $this->_redirect('project/index');
                    
                } else {
                    $proyek = $this->_proyek->getData($id);
                    
                    $id_konsumen = $proyek['id_konsumen'];
                    if($id_konsumen)
                    {
                        $konsumen = $this->_konsumenList->getData($id_konsumen);
                        $this->view->dataKonsumenDataBase = $konsumen;
                    }
                    
                    $this->view->dataGensetDataBase = $this->_gensetList->getData($id);
                    $pegawai = $this->_pegawaiList->getData($id);

                    $this->view->pegawaiDataBase = $pegawai;
                    
                    $form->populate($this->_request->getPost());
                    $this->view->pegawai = $this->getPegawai();
                    $this->view->dataKonsumen = $this->getKonsumen();
                    $this->view->dataGenset = $this->getGenset();
                }
            } else {
                $proyek = $this->_proyek->getData($id);
                $form->populate($proyek);

                $manager = $this->_pegawaiList->getDataManager($id);

                foreach ($manager as $manager);
                
                if($manager != null) {
                    $data = array( 'id_manager' => $manager['id_pegawai']);
                    $formSummary->populate($data);
                }

                $id_konsumen = $proyek['id_konsumen'];
                
                if($id_konsumen) {
                    $konsumen = $this->_konsumenList->getData($id_konsumen);
                    $this->view->dataKonsumen = $konsumen;
                }
                
                $pegawai = $this->_pegawaiList->getData($id);
                $this->view->pegawai = $pegawai;
                

                $this->view->dataGenset = $this->_gensetList->getData($id);
                
                $this->clearSession();
            }
        }
    }
    
    private function clearSession()
    {
        $this->_session->deleteSession('pegawai');
        $this->_session->deleteSession('konsumen');
        $this->_session->deleteSession('genset');
    }
    
    private function getKonsumen()
    {
        $namespace = 'konsumen';
        $result = $this->_session->getDataJoin('tabel_konsumen', 'tabel_konsumen.id_konsumen', $namespace);
        $data = array();
        foreach($result as $konsumen)
        {
            $data[] = $konsumen;
        }

        if($result)
            return $data;
        else
            return false;
    }
    
    private function getPegawai()
    {
        $namespace = 'pegawai';
        $result = $this->_session->getDataJoin('tabel_pegawai', 'tabel_pegawai.nik', $namespace);
        $data = array();
        foreach($result as $pegawai)
        {
            $data[] = $pegawai;
        }

        if($result)
            return $data;
        else
            return false;
    }
    
    private function getGenset()
    {
        $namespace = 'genset';
        $result = $this->_session->getDataJoin('tabel_genset', 'tabel_genset.id_genset', $namespace, 
                                                'tabel_spek_engine', 'tabel_spek_engine.id_engine_genset = tabel_genset.id_engine_genset');
        
        $data = array();
        foreach($result as $genset)
        {
            $data[] = $genset;
        }

        if($result)
            return $data;
        else
            return false;
    }
}